<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="105px">
      <el-row :gutter="10">
        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="用户id" prop="userId">
            <el-input v-model="form.userId" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="订单类型" prop="type">
            <el-select v-model="form.type" clearable
                       placeholder="请选择" style="width: 100%">
              <el-option
                v-for="item in dict.user_order_type"
                :key="item.dictValue"
                :label="item.dictName"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="商品id" prop="goodId">
            <el-input v-model="form.goodId" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="金额" prop="price">
            <el-input v-model="form.price" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="订单状态" prop="status">
            <el-select v-model="form.status" clearable
                       placeholder="请选择" style="width: 100%">
              <el-option
                v-for="item in dict.user_order_status"
                :key="item.dictValue"
                :label="item.dictName"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="form.remarks" autocomplete="off" type="textarea"></el-input>
          </el-form-item>
        </el-col>

      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {doInsert, doUpdate} from "@/api/shop/userorder/SysUserOrderManagementApi";
import {isNull} from "@/utils/validate";
import {formateDate} from "@/utils/format";
import {validatorRule} from "@/utils/validateRlue";

export default {
  name: "SysUserOrderManagementEdit",
  data() {

    return {
      form: {
        // 设置默认值
        version: 0
      },
      dict: {},
      rules: {
        userId: [
          {required: true, trigger: "blur", message: "用户id非空"},
        ],
        type: [
          {required: true, trigger: "blur", message: "订单类型非空"},
        ],
        goodId: [
          {required: true, trigger: "blur", message: "商品id非空"},
        ],
        price: [
          {required: true, trigger: "blur", message: "金额非空"},
          {required: false, trigger: "blur", validator: validatorRule.IS_MONEY},
        ],
        status: [
          {required: true, trigger: "blur", message: "订单状态非空"},
        ],
      },
      title: "",
      dialogFormVisible: false,
    };
  },
  created() {

  },
  mounted() {
    // 加载字典值
    this.dict.user_order_type = this.$getDictList("user_order_type");
    this.dict.user_order_status = this.$getDictList("user_order_status");
  },
  methods: {
    showEdit(row) {
      if (!row) {
        this.title = "添加";
      } else {
        this.title = "编辑";
        this.form = Object.assign({}, row);
      }
      this.dialogFormVisible = true;
    },
    close() {
      this.dialogFormVisible = false;
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
    },
    save() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          // 处理数据
          this.handlerFormData(this.form);

          // 修改
          if (!isNull(this.form.id)) {
            const {msg} = await doUpdate(this.form);
            this.$baseMessage(msg, "success");
          } else {
            const {msg} = await doInsert(this.form);
            this.$baseMessage(msg, "success");
          }

          await this.$emit("fetchData");
          this.close();
        } else {
          return false;
        }
      });
    },
    // 处理 form数据
    handlerFormData(formData) {
      if (!isNull(formData)) {
        for (let key in formData) {
          // 对于时间类进行处理
          if ("[object Date]" === Object.prototype.toString.call(formData[key])) {
            formData[key] = formateDate(formData[key], 'yyyy-MM-dd hh:mm:ss');
          }
        }
      }
    },
  },
};
</script>
